package com.szxx.suit.pair;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.szxx.constant.GdyCardTypes;
import com.szxx.domain.Card;
import com.szxx.domain.Cards;
import com.szxx.domain.GdyCard;
import com.szxx.suit.AbstractSuitTerm;
import com.szxx.suit.AbstractSuitType;
import com.szxx.suit.GdyAnalysisResult;
import com.szxx.suit.GdySuitAnalyzer;
import com.szxx.suit.GdyTypeUtil;

/**
 * 对子牌型 <br>
 *
 * @author Ambrose
 */
@Component
public class PairTypeAnalyzer extends GdySuitAnalyzer {
	
	@Override
	public AbstractSuitType getSuitType() {
		return GdyCardTypes.PAIR;
	}
	
	/** 关键数量 */
	private static int TUPLE_SIZE = 2;
	
	@Override
	protected boolean doCheckRule(Cards cards, AbstractSuitTerm term, GdyAnalysisResult result) {
		if (result.getCardNum() != TUPLE_SIZE) {
			return false;
		}
		if (result.getLaziCardNum() == 0) { // 没有癞子
			return result.getAllValueCounter().getValuesInCount(2).size() == 1;
		} else if (result.getLaziCardNum() == 1) { // 一张癞子的情况
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	protected List<Cards> doAnalysizeLaizi(Cards cards, AbstractSuitTerm term, boolean onlyMaxValue) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		GdyAnalysisResult result = (GdyAnalysisResult) cards.getResult();
		if (result.getLaziCardNum() == 1) {
			List<Card> laiziCardList = new ArrayList<Card>(cards.subList(result.getCardNum() - result.getLaziCardNum(), result.getCardNum()));
			int value = result.getNoneLaziValueCounter().getAllValues().get(0).intValue();
			for (Card card : laiziCardList) { // 设置number
				((GdyCard) card).changeCard(value);
			}
			Cards cardsCopy = cards.clone();
			analysize(cardsCopy, term);
			cardsResult.add(cardsCopy);
			return cardsResult;
		} else {
			for (Card card : cards) {
				((GdyCard) card).changeCard(card.getNumber());
			}
			Cards cardsCopy = cards.clone();
			analysize(cardsCopy, term);
			cardsResult.add(cardsCopy);
			return cardsResult;
		}
	}
	
	@Override
	protected boolean containsPossibleCards(Cards handCards, AbstractSuitTerm term) {
		GdyAnalysisResult result = (GdyAnalysisResult) handCards.getResult();
		return result.getCardNum() - result.getJokerCardNum() >= TUPLE_SIZE;
	}
	
	/** 最多使用癞子的数量 */
	private static int MAX_USE_LAIZI_CARD_NUM = 1;
	
	@Override
	protected List<Cards> doListPossibleCards(Cards handCards, AbstractSuitTerm term) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		// 非癞子部分
		GdyAnalysisResult result = (GdyAnalysisResult) handCards.getResult();
		List<Card> noneLaiziCardList = handCards.subList(0, result.getNoneCardNum());
		Cards noneLaiziCards = GdyTypeUtil.createCardsCopy(noneLaiziCardList);
		noneLaiziCards.setResult(handCards.getResult().clone());
		// 癞子部分
		List<Card> laiziCardList = handCards.subList(result.getNoneCardNum(), result.getCardNum());
		Cards laiziCards = GdyTypeUtil.createCardsCopy(laiziCardList);
		laiziCards.setResult(handCards.getResult().clone());
		
		int laiziCardNum = result.getLaziCardNum();
		int maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum ? MAX_USE_LAIZI_CARD_NUM : laiziCardNum;
		for (int useLaiziCardNum = 0; useLaiziCardNum <= maxUseLaiziCardNum; useLaiziCardNum++) {
			int needCardNum = TUPLE_SIZE - useLaiziCardNum;
			if (needCardNum > 0) { // 部分用癞子的情况
				List<Card> useLaiziCardList = GdyTypeUtil.cloneSublistedCards(laiziCards, 0, useLaiziCardNum);// 补癞子
				List<Cards> cardsList = new LinkedList<Cards>();
				if (needCardNum == 1) {
					cardsList.addAll(GdyTypeUtil.selectSingleCards(noneLaiziCards, false));
				} else {
					cardsList.addAll(GdyTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
				}
				for (Cards usecards : cardsList) {
					Card card = usecards.get(0);
					for (Card laiziCard : useLaiziCardList) {
						((GdyCard)laiziCard).changeCard(card.getValue());
					}
					Cards cardsCopy = GdyTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
					if (match(cardsCopy, term)) {
						analysize(cardsCopy, term);
						cardsResult.add(cardsCopy);
					}
				}
			} else { // 全部用癞子的情况
				List<Cards> useLaiziCardList = GdyTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
				for (Cards useLaiziCards : useLaiziCardList) {
					for (Card card : useLaiziCards) {
						((GdyCard)card).changeCard(card.getNumber());
					}
					Cards cardsCopy = GdyTypeUtil.cloneCardsAndAddCardList(useLaiziCards, null);
					if (match(cardsCopy, term)) {
						analysize(cardsCopy, term);
						cardsResult.add(cardsCopy);
					}
				}
			}
		}
		return cardsResult;
	}
	
}
